package pwd.allen.byteBuddy.methodIntercept;

import net.bytebuddy.implementation.bind.annotation.*;
import pwd.allen.byteBuddy.other.MyCallable;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.concurrent.Callable;

/**
 * 在拦截器中使用bytebuddy支持的一些注解
 *
 * @author pwdan
 * @create 2023-06-28 17:10
 **/
public class MethodIntercept3 {

    /**
     * 实现修改入参
     * @param args
     * @param callable
     * @return
     * @throws Exception
     */
    public static int delegation(@AllArguments Object[] args, @Morph MyCallable callable) throws Exception {
        System.out.println("入参数组:" + Arrays.toString(args));
        System.out.println("直接调用目标方法，返回结果为：" + callable.call(args));
        // 修改参数
        if (args != null && args.length > 0 && args[0] instanceof Integer) {
            args[0] = Integer.class.cast(args[0]) + 1;
        }
        System.out.println("修改后入参数组:" + Arrays.toString(args));
        System.out.println("直接调用目标方法，返回结果为：" + callable.call(args));
        return 100;
    }
}
